package com.hx.hospital.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hx.common.result.PageResult;
import com.hx.hospital.mapper.*;
import com.hx.hospital.pojo.Doctor;
import com.hx.hospital.service.DoctorService;
import com.hx.hospital.vo.DoctorVo;
import io.swagger.models.auth.In;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;


    /**
     * 返回全部记录
     * @return
     */
    public List<Doctor> findAll() {

        return doctorMapper.selectAll();
    }

    @Override
    public List<Doctor> findDoctorAll() {
        return doctorMapper.findDoctorAll();
    }

    @Override
    public List<Doctor> findDoctorById(Long id) {
        return doctorMapper.findDoctorById(id);
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Doctor> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Doctor> doctors = (Page<Doctor>) doctorMapper.selectAll();
        return new PageResult<Doctor>(doctors.getTotal(),doctors.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Doctor> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return doctorMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Doctor> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        Page<Doctor> doctors = (Page<Doctor>) doctorMapper.selectByExample(example);
        return new PageResult<Doctor>(doctors.getTotal(),doctors.getResult());
    }

    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Doctor findById(Long id) {
        return doctorMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     * @param doctor
     */
    public void add(Doctor doctor) {
        doctorMapper.insert(doctor);
    }

    /**
     * 修改
     * @param doctor
     */
    public void update(Doctor doctor) {
        doctorMapper.updateByPrimaryKeySelective(doctor);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(Long id) {
        doctorMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Doctor.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 医生名字
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            // 职位
            if(searchMap.get("position")!=null && !"".equals(searchMap.get("position"))){
                criteria.andLike("position","%"+searchMap.get("position")+"%");
            }
            // 主治病症
            if(searchMap.get("IndicationsId")!=null && !"".equals(searchMap.get("IndicationsId"))){
                criteria.andLike("IndicationsId","%"+searchMap.get("IndicationsId")+"%");
            }
            // 医生照片
            if(searchMap.get("doctorPhoto")!=null && !"".equals(searchMap.get("doctorPhoto"))){
                criteria.andLike("doctorPhoto","%"+searchMap.get("doctorPhoto")+"%");
            }
            // 医生描述
            if(searchMap.get("describe")!=null && !"".equals(searchMap.get("describe"))){
                criteria.andLike("describe","%"+searchMap.get("describe")+"%");
            }


        }
        return example;
    }

}
